package com.datareport.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.datareport.data.entity.DerivedInfo;
import com.datareport.data.entity.DisabilityInfoNow;
import com.datareport.data.mapper.DerivedInfoMapper;
import com.datareport.data.mapper.DisabilityInfoNowMapper;
import com.datareport.data.service.DerivedInfoService;
import com.datareport.data.service.DisabilityInfoNowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author datareport
 * @since 2021-10-24
 */
@Service
public class DisabilityInfoNowServiceImpl extends ServiceImpl<DisabilityInfoNowMapper, DisabilityInfoNow> implements DisabilityInfoNowService {

   @Autowired
   private DisabilityInfoNowMapper disabilityInfoNowMapper;

   @Autowired
    DerivedInfoMapper derivedInfoMapper;

   @Autowired
   DisabilityInfoNowService disabilityInfoNowService;

   @Autowired
   DerivedInfoService derivedInfoService;

    @Override
    public void saveList(List<DisabilityInfoNow> list) {

        int m=list.size();
        for(int i=0;i<m;i++) {

            DisabilityInfoNow disabilityInfoNow = list.get(i);

            try {
                    disabilityInfoNowService.saveOrUpdate(disabilityInfoNow);
            }catch (Exception e){
                e.printStackTrace();
            }


            /**
             * 正则提取信息
             */
            DerivedInfo derivedInfo = new DerivedInfo();
            //获取残疾证号
            String disabilityId = list.get(i).getId();


            //编写正则表达式
            //1,2位分别是省.自治区或直辖市。
            //3.4两位表示所在的市
            //5.6两位表示所以的县区。
            //7-14位表示出生年月日。
            //15.16位表示所在地派出所的代码，
            //19位数字为残疾类别编号。  １：视力残疾；２：听力残疾；３：言语残疾；４：肢体残疾；５：智力残疾；６：精神残疾；７：多重残疾：存在2项或2项以上残疾。
            //20位残疾等级编号。        1：一级 2：二级 3：三级 4：四级
            String pattern1 = "^(\\d\\d)(\\d\\d)(\\d\\d)";  //获取省，市，区
            String pattern2 = "^(\\d{6})(\\d{8})";           //获取出生年月日
            String pattern3 = "^(\\d{6})(\\d{8})(\\d\\d)";  //获取所在地派出所的代码
            String pattern4 = "^(\\d{6})(\\d{8})(\\d\\d)(\\d.)(\\d)";       //获取残疾类别
            String pattern5 = "^(\\d{6})(\\d{8})(\\d\\d)(\\d.)(\\d)(\\d)";  //获取残疾等级
            //匹配
            Pattern r1 = Pattern.compile(pattern1);
            Pattern r2 = Pattern.compile(pattern2);
            Pattern r3 = Pattern.compile(pattern3);
            Pattern r4 = Pattern.compile(pattern4);
            Pattern r5 = Pattern.compile(pattern5);

            Matcher m1 = r1.matcher(disabilityId);
            Matcher m2 = r2.matcher(disabilityId);
            Matcher m3 = r3.matcher(disabilityId);
            Matcher m4 = r4.matcher(disabilityId);
            Matcher m5 = r5.matcher(disabilityId);

//        if(m1.find()){
//            System.out.println("省: " + m1.group(1) );
//            System.out.println("市：" + m1.group(2) );
//            System.out.println("区：" + m1.group(3) );
//        }
//        if(m2.find()){
//            System.out.println("所属区划: " + m2.group(1) );
//            System.out.println("出生年月日: " + m2.group(2) );
//        }
//        if(m3.find()){
//            System.out.println("委员会: " + m3.group(3) );
//        }
//        if(m4.find()){
//            System.out.println("残疾类别: " + m4.group(5) );
//        }
//        if(m5.find()){
//            System.out.println("残疾等级: " + m5.group(6) );
//        }

            //计算年龄
            String age = "0";
            if (m1.find() && m2.find()&& m3.find() && m4.find() && m5.find()) {
                String birthday = m2.group(2);
                String pattern6 = "^(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)";
                Pattern r6 = Pattern.compile(pattern6);
                Matcher m6 = r6.matcher(birthday);
                if (m6.find()) {
                    int Year = Integer.parseInt(m6.group(1));
                    int Month = Integer.parseInt(m6.group(2));
                    int Day = Integer.parseInt(m6.group(3));
                    // 得到当前时间的年
                    Calendar cal = Calendar.getInstance();
                    int yearNow = cal.get(Calendar.YEAR);
                    int monthNow = 9;
                    int dayNow = 30;
                    // 用当前年月日减去生日年月日
                    int yearMinus = yearNow - Year;
                    int monthMinus = monthNow - Month;
                    int dayMinus = dayNow - Day;

                    int trueage = yearMinus;// 先大致赋值
                    if (yearMinus < 0) {// 选了未来的年份
                        trueage = 0;
                    } else if (yearMinus == 0) {// 同年的，要么为1，要么为0
                        if (monthMinus < 0) {// 选了未来的月份
                            trueage = 0;
                        } else if (monthMinus == 0) {// 同月份的
                            if (dayMinus < 0) {// 选了未来的日期
                                trueage = 0;
                            } else if (dayMinus >= 0) {
                                trueage = 1;
                            }
                        } else if (monthMinus > 0) {
                            trueage = 1;
                        }
                    } else if (yearMinus > 0) {
                        if (monthMinus < 0) {// 当前月>生日月
                        } else if (monthMinus == 0) {// 同月份的，再根据日期计算年龄
                            if (dayMinus < 0) {
                            } else if (dayMinus >= 0) {
                                trueage = trueage + 1;
                            }
                        } else if (monthMinus > 0) {
                            trueage = trueage + 1;
                        }
                    }
                    age = String.valueOf(trueage);
                }

                derivedInfo.setId(list.get(i).getId());
                derivedInfo.setRegionId(list.get(i).getRegionId());
                derivedInfo.setName(list.get(i).getName());
                derivedInfo.setAge(Integer.parseInt(age));
                derivedInfo.setDisabilityCategory(Integer.parseInt(m4.group(5)));
                derivedInfo.setDisabilityLevel(Integer.parseInt(m5.group(6)));
                derivedInfo.setProvince(Integer.parseInt(m1.group(1)));
                derivedInfo.setCity(Integer.parseInt(m1.group(2)));
                derivedInfo.setTown(Integer.parseInt(m1.group(3)));
                derivedInfo.setCommunity(Integer.parseInt(m3.group(3)));
                try {
                    derivedInfoService.saveOrUpdate(derivedInfo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public Page<DisabilityInfoNow> selectAll(Integer pageNum, Integer pageSize) {
        Page<DisabilityInfoNow> page = new Page<>(pageNum, pageSize);

        Page<DisabilityInfoNow> pageParams = disabilityInfoNowMapper.selectPage(page, null);

        return pageParams;
    }
}
